﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Business;

namespace ServicesDirectory.UserControl
{
    public partial class OrganisationDetail3 : System.Web.UI.UserControl
    {
        
        public string EoiProgrammes;
        public string EoiService;
        /// <summary>
        /// Occurs when [set data detail3].
        /// </summary>
        public event EventHandler SetDataDetail3;
        /// <summary>
        /// Gets the data.
        /// </summary>
        public void GetData()
        {

                EoiProgrammes = GetSelectedValueCheckListBox(cklProgrammes);
                EoiService = GetSelectedValueCheckListBox(cklServices);
        }
        /// <summary>
        /// Loads the data.
        /// </summary>
        public void LoadData()
        {
            SetSelectedValueCheckListBox(cklProgrammes, EoiProgrammes);
            SetSelectedValueCheckListBox(cklServices, EoiService);
        }


        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindEoiProgrammes();
                BindEoiServices();
                if (SetDataDetail3!=null)
                    SetDataDetail3(sender, e);
            }
        }
        /// <summary>
        /// Gets the selected value check list box.
        /// </summary>
        /// <param name="checkBoxList">The check box list.</param>
        /// <returns></returns>
        private string GetSelectedValueCheckListBox(CheckBoxList checkBoxList)
        {
            var result = "";
            foreach (ListItem item in checkBoxList.Items)
            {
                if (item.Selected)
                    result += item.Value + "#";
            }
            return result.Length - 2 > -1 ? result.Substring(0, result.Length - 2) : result;
        }
        /// <summary>
        /// Sets the selected value check list box.
        /// </summary>
        /// <param name="checkBoxList">The check box list.</param>
        /// <param name="values">The values.</param>
        private void SetSelectedValueCheckListBox(CheckBoxList checkBoxList, string values)
        {
            var value = values.Split('#').ToList();
            foreach (ListItem item in checkBoxList.Items)
            {
                if (value.Contains(item.Value))
                    item.Selected = true;
            }
        }

        #region load reference data

        /// <summary>
        /// Binds the eoi programmes.
        /// </summary>
        private void BindEoiProgrammes()
        {
            cklProgrammes.DataSource = OrganizationBusiness.Instance.GetEoiProgrammes();
            cklProgrammes.DataValueField = "RefValue";
            cklProgrammes.DataTextField = "DataRef";
            cklProgrammes.DataBind();
        }

        /// <summary>
        /// Binds the eoi services.
        /// </summary>
        private void BindEoiServices()
        {
            cklServices.DataSource = OrganizationBusiness.Instance.GetEoiServices();
            cklServices.DataTextField = "DataRef";
            cklServices.DataValueField = "RefValue";
            cklServices.DataBind();
        }
        #endregion
    }
}